package com.hzit.sb2.util;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.StringJoiner;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.interceptor.KeyGenerator;

public class CustomKeyGenerator implements KeyGenerator {

    private static final Logger logger = LoggerFactory.getLogger(CustomKeyGenerator.class);

    @Override
    public Object generate(Object target, Method method, Object... params) {

        String className = method.getDeclaringClass().getName();
        String methodName = method.getName();
        String paramHash = String.valueOf(Arrays.toString(params).hashCode());
        String cacheKey = new StringJoiner("_").add(className).add(methodName)
                .add(paramHash).toString();
        logger.debug("generate cache key : {}", cacheKey);

        return cacheKey;
    }
}